import { Request, Response } from 'express';
import goodsService from '../../services/goods.service';
export class Controller {
    // 获取所有类别接口
    get(_req: Request, res: Response): void {
        goodsService.getAllCategory()
            .then((data) => {
                res.status(200).json({ msg: 'ok', datas: data });
            })
            .catch((error) => {
                res.status(200).json({ msg: 'error', error: error });
            });
    }
    // 新增类别接口
    add(req: Request, res: Response): void {
        // 解析请求参数
        const { name, description, sort } = req.body;
        goodsService.addCategory({ name, description, sort })
            .then(() => {
                res.status(200).json({ msg: 'ok' });
            })
            .catch((error) => {
                res.status(200).json({ msg: 'error', error: error });
            });
    }
    // 更新类别接口
    update(req: Request, res: Response): void {
        const { name, id, description, sort } = req.body;
        let sortValue = sort;
        if (sortValue === undefined) {
            sortValue = 0;
        }
        if (!id) {
            res.status(200).json({ msg: 'error', error: '请指定要更新的类别 id!' });
            return;
        }
        goodsService.updateCategory({ name, id, description, sort: sortValue })
            .then(() => {
                res.status(200).json({ msg: 'ok' });
            })
            .catch((error) => {
                res.status(200).json({ msg: 'error', error: error });
            });
    }
    // 新增商品接口
    goodsAdd(req: Request, res: Response): void {
        const { 
            name, description, detail, category, 
            price, discounted_price, stock, 
            image, status, brand 
        } = req.body;
        goodsService.addGoods({
            name,
            description,
            detail,
            category_id: category.id,
            price,
            discounted_price,
            stock,
            image,
            status,
            brand
        })
        .then(() => {
            res.status(200).json({ msg: 'ok' });
        })
        .catch((error) => {
            res.status(200).json({ msg: 'error', error: error });
        });
    }
    // 更新商品接口
    goodsUpdate(req: Request, res: Response): void {
        const { 
            id, name, description, detail, category, 
            price, discounted_price, stock, 
            image, status, brand 
        } = req.body;
        goodsService.updateGoods({
            id,
            name,
            description,
            detail,
            category_id: category.id,
            price,
            discounted_price,
            stock,
            image,
            status,
            brand
        })
        .then(() => {
            res.status(200).json({ msg: 'ok' });
        })
        .catch((error) => {
            res.status(200).json({ msg: 'error', error: error });
        });
    }
    // 删除商品接口
    goodsDelete(req: Request, res: Response): void {
        const { id } = req.body;
        goodsService.deleteGoods(id)
        .then(() => {
            res.status(200).json({ msg: 'ok' });
        })
        .catch((error) => {
            res.status(200).json({ msg: 'error', error: error });
        });
    }
    // 获取商品详情接口
    goodsGet(req: Request, res: Response): void {
        const id = Number(req.query.id as string);
        goodsService.getGoodsDetail(id)
        .then((data) => {
            res.status(200).json({ msg: 'ok', datas: data });
        })
        .catch((error) => {
            res.status(200).json({ msg: 'error', error: error });
        });
    }
    // 查询商品列表接口
    goodsListGet(req: Request, res: Response): void {
        let cid: number | undefined = undefined;
        if (req.query.cid) {
            cid = Number(req.query.cid as string);
        }
        const offset = Number(req.query.offset as string);
        const limit = Number(req.query.limit as string);
        goodsService.getGoodsList(cid, offset, limit)
        .then((data) => {
            res.status(200).json({ msg: 'ok', datas: data });
        })
        .catch((error) => {
            res.status(200).json({ msg: 'error', error: error });
        });
    }
    goodsListSearch(req: Request, res: Response): void {
        // 获取请求中的关键词，分页参数
        let keyword = req.query.keyword as string
        let offset = Number(req.query.offset as string)
        let limit = Number(req.query.limit as string)
        goodsService.searchGoods(keyword, offset, limit).then((data)=>{
            res.status(200).json({ msg: 'ok',datas: data })
        }).catch((error)=>{
            res.status(200).json({ msg: 'error', error: error })
        })
    }
}
export default new Controller();